export default {
	friendlyDate(timestamp) {
		var formats = {
			'year': '%n% 年前',
			'month': '%n% 月前',
			'day': '%n% 天前',
			'hour': '%n% 小时前',
			'minute': '%n% 分钟前',
			'second': '%n% 秒前',
		};
	
		var now = Date.now();
		var seconds = Math.floor((now - timestamp) / 1000);
		var minutes = Math.floor(seconds / 60);
		var hours = Math.floor(minutes / 60);
		var days = Math.floor(hours / 24);
		var months = Math.floor(days / 30);
		var years = Math.floor(months / 12);
	
		var diffType = '';
		var diffValue = 0;
		if (years > 0) {
			diffType = 'year';
			diffValue = years;
		} else {
			if (months > 0) {
				diffType = 'month';
				diffValue = months;
			} else {
				if (days > 0) {
					diffType = 'day';
					diffValue = days;
				} else {
					if (hours > 0) {
						diffType = 'hour';
						diffValue = hours;
					} else {
						if (minutes > 0) {
							diffType = 'minute';
							diffValue = minutes;
						} else {
							diffType = 'second';
							diffValue = seconds === 0 ? (seconds = 1) : seconds;
						}
					}
				}
			}
		}
		return formats[diffType].replace('%n%', diffValue);
	},
	/**
	 * 显示提示框
	 * 提示信息默认2s
	 */
	toast(title, icon = 'none', duration = 2500) {
		uni.showToast({
			title,
			icon,
			duration: 2500
		});
	},
	/**
	 * 统一跳转接口,拦截未登录路由
	 * navigator标签现在默认没有转场动画，所以用view
	 */
	navTo(url) {
		let authPages = [ // 进入如下页面是，需要进行登录授权验证
			'/pages/cart/cart', 
			'/pages/user/user', 
			'/pages/order/createOrder',
		]
		// debugger
		// 校验 含有需要授权登录的页面标记
		let canotPass = authPages.some(item => url.indexOf(item) > -1)
		if (canotPass && !uni.getStorageSync('accessToken')) {
			// 显示授权登录组件
			uni.navigateTo({
				url: '/pages/public/wxLoginAuth'
			})
		}else{
			uni.showLoading({
				title: '页面跳转中...'
			});
			uni.navigateTo({
				url
			})
			setTimeout(()=>{
				uni.hideLoading();
			}, 1000)
		}
	},
	redirectTo(url){
		uni.redirectTo({
			url
		})
	},
	/**
	 * 刷新当前onload方法，类似刷新页面效果
	 * @param {string} type ：'ON_SHOW' 
	 */
	refreshPage(type) {
		let pages = getCurrentPages()
		let page = pages[pages.length - 1]
		console.log('刷新当前页面page信息', page)
		page.onLoad() // 默认出发页面的onLoad方法
	},
	/**
	 * 刷新前一页onload方法，类似刷新页面效果
	 */
	refreshPrePage(type) {
		let pages = getCurrentPages()
		let page = pages[pages.length - 2]
		console.log('刷新上一页面page信息', page)
		page.onLoad() // 默认出发页面的onLoad方法
	},
	/**
	 * 返回上一页的实例
	 */
	prePage(){
		let pages = getCurrentPages();
		let prePage = pages[pages.length - 2];
		// #ifdef H5
		return prePage;
		// #endif
		return prePage.$vm;
	},
	/**
	 * 回到顶部
	 */
	goTop() {
		uni.pageScrollTo({
			duration: 300,
			scrollTop: 0
		})
	},
	/**
	 * 确认提示框
	 */
	confirmModal(config) {
		uni.showModal({
			title: config.title || '信息提示',
			content: config.content || '确认要进行接下来的操作吗',
			// showCancel: config.cancel || false,
			confirmText: config.confirmText || '确定',
			cancelText: config.cancelText ||'取消',
			success: function(res) {
				if (res.confirm) {
					config.success && config.success(true)
				} else if (res.cancel) {
					config.success && config.success(false)
				}
			}
		});
	},
	/**
	 * @param {Object} type
	 */
	getDate(type) {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
		console.log(`${year}-${month}-${day}`)
		return `${year}-${month}-${day}`;
	},
	getDateTime() {
		var date = new Date();
		var seperator1 = "-";

		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var hour = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		var strDate = date.getDate();

		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = year + seperator1 + month + seperator1 + strDate + " " + hour + ":" + minutes + ":" + seconds;

		return currentdate;
	},
	/**
	 * 获取登录成功后的accessToken
	 */
	getToken(){
		return uni.getStorageSync('accessToken')
	},
	
	/**
	 * 
	 * @functionName: getParameter
	 * @Description: get parameter from URL 
	 * @author: Double
	 * 
	 */
	getParaByUrl(url, name) {
		var search = url;
		if(!search) {
			return false;
		}
		search = search.split('?')
		var data = search[1].split('=');
		if(search[1].indexOf(name) == (-1)) {
		    return '';
		}
		if(search[1].indexOf('&') == (-1)) {
		    data = search[1].split('=');
		    return data[1];
		} else {
		    data = search[1].split('&'); 
		    for(var i = 0; i <= data.length - 1; i++){
		    	var l_data=data[i].split('=');
		        if(l_data[0] == name) {
		        	return l_data[1];
		        	break;
		        } else {
		        	continue;
		        }
		    }
		}
	}
}

